//
//  SEPrescriptionOrderListViewController.swift
//  SmartEyeForPatient
//
//  Created by qdsg on 2021/4/16.
//

import UIKit

class SEPrescriptionOrderListViewController: SEBaseViewController {

    @IBOutlet weak var tableView: UITableView! {
        didSet {
            tableView.tableFooterView = UIView()
            tableView.se_header = SERefreshHeader { [weak self] in self?.loadNewData() }
            tableView.se_footer = SERefreshFooter { [weak self] in self?.loadMoreData() }
            tableView.se_empty = SEEmptyView { [weak self] in self?.requestPrescriptionOrderList() }
            tableView.register(UINib(nibName: SEPrescriptionOrderListCell.className, bundle: nil), forCellReuseIdentifier: SEPrescriptionOrderListCell.reuseId)
        }
    }

    var viewModel = SEPrescriptionOrderListlViewModel()

    convenience init(prescriptionIds: [String]) {
        self.init()
        self.viewModel.prescriptionIds = prescriptionIds
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        title = "处方订单"
        requestPrescriptionOrderList()
        // Do any additional setup after loading the view.
    }

    override func setupUI() {
        super.setupUI()

        viewModel.updateData = { [weak self] status in
            self?.endRefreshing()
            self?.tableView.se_empty?.allowShow = true
            switch status {
            case .success:
                self?.tableView.reloadData()
            case .auth(let msg):
                self?.presentLoginViewController(selector: #selector(self?.requestPrescriptionOrderList)) {
                    SEHUD.showError(message: msg)
                }
            case .failure(let msg):
                SEHUD.showError(message: msg)
            }
        }

        viewModel.cancelHandler = { status in
            self.endRefreshing()
            switch status {
            case .success:
                self.requestPrescriptionOrderList()
            case .auth(let msg):
                self.presentLoginViewController(selector: #selector(self.requesetCancelPrescriptionOrder)) {
                    SEHUD.showError(message: msg)
                }
            case .failure(let msg):
                SEHUD.showError(message: msg)
            }
        }
    }

    private func pushToPrescriptionOrderDetail() {
        let vc = SEPrescriptionOrderDetailViewController(prescriptionId: viewModel.selectedViewModel.model.id)
        vc.paySuccessHandler = {
            self.requestPrescriptionOrderList()
        }
        navigationController?.pushViewController(vc, animated: true)
    }
}

// MARK: - UITableViewDataSource, UITableViewDelegate

extension SEPrescriptionOrderListViewController: UITableViewDataSource, UITableViewDelegate {

    // MARK: - UITableViewDataSource

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 1
    }

    func numberOfSections(in tableView: UITableView) -> Int {
        return viewModel.models.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: SEPrescriptionOrderListCell.reuseId, for: indexPath) as! SEPrescriptionOrderListCell
        cell.delegate = self
        cell.updateData(viewModel.listViewModel(indexPath: indexPath), indexPath: indexPath)
        return cell
    }

    // MARK: - UITableViewDelegate
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 10
    }

    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let view = UIView()
        view.backgroundColor = UIColor.clear
        return view
    }

}

// MARK: - SEPrescriptionOrderListCellDelegate

extension SEPrescriptionOrderListViewController: SEPrescriptionOrderListCellDelegate {
    func prescriptionOrderListCellBtnAction(type: SEPrescriptionOrderListCellActionType, indexPath: IndexPath) {
        let vm = viewModel.listViewModel(indexPath: indexPath)
        viewModel.selectedViewModel = vm
        switch type {
        case .cancel:
            requesetCancelPrescriptionOrder()
        case .pay:
            pushToPrescriptionOrderDetail()
        case .logistics:
            SELog("logistics")
        case .detail:
            pushToPrescriptionOrderDetail()
        }
    }
}

// MARK: - APIRequest
extension SEPrescriptionOrderListViewController {
    @objc private func requestPrescriptionOrderList() {
        tableView.se_header?.beginRefreshing()
    }

    private func endRefreshing() {
        tableView.se_footer_hidden = false
        tableView.endHeaderRefreshing()
        tableView.endFooterRefreshing(hasMore: viewModel.hasMore)
    }

    private func loadNewData() {
        viewModel.page = 1
        viewModel.requestPrescriptionList()
    }

    private func loadMoreData() {
        viewModel.page += 1
        viewModel.requestPrescriptionList()
    }

    @objc private func requesetCancelPrescriptionOrder() {
        viewModel.requesetCancelPrescriptionOrder()
    }

}
